//
//  recommendViewController.m
//  autoHome
//
//  Created by qianfeng on 15/9/9.
//  Copyright (c) 2015年 qf1523. All rights reserved.
//

#import "recommendViewController.h"
#import "headView.h"
#import "changeView.h"
#import "Header.h"
#import "TouchModel.h"
#import "OtherViewController.h"
#import "newsViewController.h"
@interface recommendViewController ()<UIScrollViewDelegate>{
    

    headView * headV;
    changeView * changeV;
    UIScrollView * everyView;
    NSMutableArray * headArray;
}

@end

@implementation recommendViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
//    self.extendedLayoutIncludesOpaqueBars = NO;
//    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    headArray = [[NSMutableArray alloc]init];
    [self writeData];
   
    self.modalPresentationCapturesStatusBarAppearance = YES;
    [self creatHeadBtn];


}
//从文件中得到以选中的button
- (void)writeData{
    
    if (everyView==nil) {
        everyView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64-49)];
        everyView.contentOffset = CGPointZero;
        everyView.showsHorizontalScrollIndicator = NO;
        everyView.showsVerticalScrollIndicator = NO;
        everyView.delegate = self;
        everyView.pagingEnabled = YES;
        everyView.bounces = NO;
        [self.view addSubview:everyView];
    }
    NSString * string = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * filePath = [string stringByAppendingString:@"/selectedBtn.data"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray * array1 = [NSArray arrayWithObjects:headButton, nil];
        NSArray * array2 = [NSArray arrayWithObjects:headUrl, nil];
        for (int i = 0; i < array1.count; i ++) {
            TouchModel * model = [[TouchModel alloc]initWithTitle:array1[i] AndUrlString:array2[i]];
            [headArray addObject:model];
            NSData * data = [NSKeyedArchiver archivedDataWithRootObject:headArray];
            [data writeToFile:filePath atomically:YES];
        }
        everyView.contentSize = CGSizeMake(headArray.count*self.view.frame.size.width, self.view.frame.size.height-64-49);
        [self createShowView];
    }
    else{
        [headArray removeAllObjects];
        headArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        everyView.contentSize = CGSizeMake(headArray.count*self.view.frame.size.width, self.view.frame.size.height-64-49);
        [self createShowView];
        
    }
}
- (void)createShowView{
    NSInteger showCount = everyView.contentSize.width/self.view.frame.size.width;
//    for (int i = 0; i < showCount; i ++) {
//        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
//        [everyView addSubview:view];
//    }
    for (int i = 0; i < showCount; i ++) {
        TouchModel * model = headArray[i];
        NSString * title = model.title;
        NSString * url = model.urlString;
        if ([title isEqualToString:@"最新"]) {
            newsViewController * news = [[newsViewController alloc]init];
            news.url = url;
            [news.view setFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
            [everyView addSubview:news.view];
            [self addChildViewController:news];
        }
        else if([title isEqualToString:@"快报"]){
            
          }
        else{
            OtherViewController * other = [[OtherViewController alloc]init];
            other.url = url;
            [other.view setFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
            [everyView addSubview:other.view];
            [self addChildViewController:other];

        }
    }
}
- (void)creatHeadBtn{
    if (changeV == nil) {
        changeV = [[changeView alloc]initWithFrame:CGRectMake(0,-self.view.bounds.size.height/2, self.view.bounds.size.width, self.view.bounds.size.height/2-50)];

        [changeV addObserver:self forKeyPath:@"pressTag" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:@"changeV"];
        [changeV creatButton];
        [self.view addSubview:changeV];
    }
    if (headV == nil) {
        headV = [[headView alloc]initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, 40)];
        [headV creatScrollView];
        [headV addObserver:self forKeyPath:@"pressIndex" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:@"headV"];
        self.automaticallyAdjustsScrollViewInsets = NO;
        [self.view addSubview:headV];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    NSString * str = (__bridge NSString*)context;
    if ([[change objectForKey:@"new"]intValue]==100&&[str isEqualToString:@"headV"]) {
        [UIView animateWithDuration:0.5 animations:^{
            [headV setFrame:CGRectMake(self.view.frame.size.width, 30, 0, 0)];
        }];
        [UIView animateWithDuration:0.5 delay:0.5 options: UIViewAnimationOptionLayoutSubviews animations:^{
            self.navigationController.navigationBarHidden = NO;
            [changeV setFrame:CGRectMake( 0 , 0, self.view.bounds.size.width, self.view.bounds.size.height/2-50)];
            
        } completion:^(BOOL finished) {
        }];
    }
    
    else if([[change objectForKey:@"new"]intValue]==100&&[str isEqualToString:@"changeV"]){
        [UIView animateWithDuration:0.5 animations:^{
              [changeV setFrame:CGRectMake(0,-self.view.bounds.size.height/2, self.view.bounds.size.width, self.view.bounds.size.height/2-50)];
              self.navigationController.navigationBarHidden = YES;
        }completion:^(BOOL finished) {
             [headV creatScrollView];
        }];

        [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
            [headV setFrame:CGRectMake(10, 30, self.view.frame.size.width-20, 40)];
        } completion:^(BOOL finished) {
            [self writeData];
        }];
    }
}
#pragma **ScrollView的代理**

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
